package com.ev.apis.controller.asl;

import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.ev.framework.annotation.EvApiByToken;
import com.ev.framework.utils.DateUtils;
import com.ev.framework.utils.JSONUtils;
import com.ev.framework.utils.R;
import com.ev.k3cloudBusiness.service.K3FinalGoodsInStockSiteAsIsService;
import com.ev.k3cloudBusiness.vo.ExpectedStatusInTheLibraryVo;
import com.ev.k3cloudBusiness.vo.FinalGoodsInStockSiteAsIsVo;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 成品在库位置现况API
 * @author 姚家乐
 * @since 2021/8/3 13:06
 */
@Deprecated
@Api(value = "/", tags = "成品在库位置现况API")
@RestController
public class FinalGoodsInStockSiteAsIsController {

    @Autowired
    private K3FinalGoodsInStockSiteAsIsService service;

    /**
     * 成品在库位置现况
     *
     * @date 2019-11-27
     * @author gumingjie
     */
    @EvApiByToken(value = "/apis/finalGoodsInStockSiteAsIs/query", method = RequestMethod.POST, apiTitle = "成品在库位置现况")
    @ApiOperation("成品在库位置现况")
    public R queryMaterialOutboundDetail(
            @ApiParam(value = "起始时间") @RequestParam(value = "dateStart", defaultValue = "", required = false) Date dateStart,
            @ApiParam(value = "结束时间") @RequestParam(value = "dateEnd", defaultValue = "", required = false) Date dateEnd,
            @ApiParam(value = "物料代码") @RequestParam(value = "materialNumber", defaultValue = "", required = false) String materialNumber,
            @ApiParam(value = "仓库") @RequestParam(value = "stockName", defaultValue = "", required = false) String stockName,
            @ApiParam(value = "当前第几页", required = true) @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @ApiParam(value = "一页多少条", required = true) @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize
    ) {
        Map<String,String> param = Maps.newHashMap();
        param.put("stockName",stockName);
        param.put("materialNumber",materialNumber);
        param.put("dateStart", DateUtils.format(dateStart));
        param.put("dateEnd",DateUtils.format(dateEnd));
        param.put("pageNo",String.valueOf(pageNo));
        param.put("pageSize",String.valueOf(pageSize));
        param.put("flag","1");
        return service.finalGoodsInStockSiteAsIsSync(param);
    }

    /**
     * 成品在库位置现况列表导出
     *
     * @date 2019-11-27
     * @author gumingjie
     */
    @EvApiByToken(value = "/apis/finalGoodsInStockSiteAsIs/exportExcel", method = RequestMethod.GET, apiTitle = "成品在库位置现况列表导出")
    @ApiOperation("成品在库位置现况列表导出")
    public void exportFinalGoodsInStockSiteAsIs(
            @ApiParam(value = "起始时间") @RequestParam(value = "dateStart", defaultValue = "", required = false) Date dateStart,
            @ApiParam(value = "结束时间") @RequestParam(value = "dateEnd", defaultValue = "", required = false) Date dateEnd,
            @ApiParam(value = "物料代码") @RequestParam(value = "materialNumber", defaultValue = "", required = false) String materialNumber,
            @ApiParam(value = "仓库") @RequestParam(value = "stockName", defaultValue = "", required = false) String stockName,
            HttpServletRequest request, HttpServletResponse response,
                                                 ModelMap modelMap) {
        Map<String, String> param = Maps.newHashMap();
        param.put("stockName",stockName);
        param.put("materialNumber",materialNumber);
        param.put("dateStart", DateUtils.format(dateStart));
        param.put("dateEnd",DateUtils.format(dateEnd));
        param.put("flag", "0");

        List<FinalGoodsInStockSiteAsIsVo> data = (List<FinalGoodsInStockSiteAsIsVo>) service.finalGoodsInStockSiteAsIsSync(param).get("data");
        if (!CollectionUtils.isEmpty(data)) {
            Map<String, Object> map = Maps.newHashMap();
            map.put("list", JSONUtils.format(data));
            ClassPathResource classPathResource = new ClassPathResource("poi/final_goods_in_stock_site_as_is.xlsx");
            TemplateExportParams result = new TemplateExportParams(classPathResource.getPath());
            modelMap.put(TemplateExcelConstants.FILE_NAME, "成品在库位置现况");
            modelMap.put(TemplateExcelConstants.PARAMS, result);
            modelMap.put(TemplateExcelConstants.MAP_DATA, map);
            PoiBaseView.render(modelMap, request, response,
                    TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
        }

    }
}
